package org.kele.sc2020.proivder.controller;

import lombok.extern.slf4j.Slf4j;
import org.kele.sc2020.common.dto.KeleResult;
import org.kele.sc2020.proivder.service.ShardGroupTaskEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class ShardGroupController {

    @Autowired
    private ShardGroupTaskEngine shardGroupTaskEngine;

    /**
     * 处理大任务入口
     * <p>
     * 1.计算实例数量,校验,兵返回可用实例集合;
     * 2.依据实例数量拆分子任务
     * 3.异步调用子任务接口
     * 4.汇总返回
     *
     * @return
     */
    @GetMapping(value = "/shard", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public KeleResult<Object> shardTask() {
        String result = shardGroupTaskEngine.handleGroup(1);
        return KeleResult.success(result);
    }

    /**
     * 处理某一个小组任务
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/shard/group-task", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public String shardTaskGroup(@RequestBody List<Integer> ids) {
        return shardGroupTaskEngine.handelSubGroup(ids);
    }
}
